import { defineStore } from 'pinia'
//引入封装好的接口
import { reqLogin, getInfo, } from "@/api/user/index"

import type { loginFormData, state } from "./type"
//引入储存token的接口
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/token'
//引入路由


export const store = defineStore('shop', {
    state: (): state => ({
        token: GET_TOKEN(),  // token
        adminName: '',

    }),
    actions: {
        async userLogin(data: loginFormData) {
            let res = await reqLogin(data)
            if (res.code === 200) {
                this.token = (res.data.token as string)
                SET_TOKEN(this.token)
                return Promise.resolve("ok")
            } else {
                return Promise.reject(new Error(res.data.messages))
            }

        },
        async userInfo() {
            let res = await getInfo()
            if (res.code == 200) {
                this.adminName = res.data.checkUser.adminName
                return Promise.resolve("ok")
            } else {
                return Promise.reject("获取信息失败")
            }
        },
        
    },
    getters: {}
})